package com.tepth.vehiclemaintenance.mvp.work.content;

import com.tepth.vehiclemaintenance.bean.MaintainDriverRemark;
import com.tepth.vehiclemaintenance.bean.MaintainListWork;
import com.tepth.vehiclemaintenance.net.ApiModel;
import com.tepth.vehiclemaintenance.net.exception.NoNetworkException;
import com.tepth.vehiclemaintenance.net.exception.TokenInvalidException;
import com.tepth.vehiclemaintenance.net.observer.SimpleDisposableObserver;

import java.net.SocketTimeoutException;
import java.util.ArrayList;

/**
 * Created by TMVPHelper on 2017/05/11
 */
public class ContentPresenter extends ContentContract.Presenter {

    @Override
    void getDriverRemark() {
        mRxManager.add(mModel.getDriverRemark(mView.getAuthKey(), mView.getMaintenanceId())
                .subscribeWith(new SimpleDisposableObserver<ApiModel<ArrayList<MaintainDriverRemark>>>(mView) {
                    @Override
                    public void onSucceed(Object o) {

                    }

                    @Override
                    public void onNext(ApiModel<ArrayList<MaintainDriverRemark>> value) {
                        mView.showDriverRemarkList(value.getResult());
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof NoNetworkException) {
                            mView.onLoadTagDataError(e.getMessage());
                        } else if (e instanceof TokenInvalidException) {
                            mView.onLoadTagDataError(e.getMessage());
                        } else if (e instanceof SocketTimeoutException) {
                            mView.onLoadTagDataError("服务器连接超时!");
                        } else if (e instanceof IllegalStateException) {
                            mView.onLoadTagDataError("数据格式错误!");
                        } else {
                            mView.onLoadTagDataError(e.getMessage());
                        }
                    }
                }));
    }

    @Override
    void getMaintainListWork() {
        mRxManager.add(mModel.getMaintainListWork(mView.getAuthKey(), mView.getMaintenanceId())
                .subscribeWith(new SimpleDisposableObserver<ApiModel<ArrayList<MaintainListWork>>>(mView) {
                    @Override
                    public void onSucceed(Object o) {

                    }

                    @Override
                    public void onNext(ApiModel<ArrayList<MaintainListWork>> value) {
                        mView.showMaintainListWork(value.getResult());
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof NoNetworkException) {
                            mView.onLoadWorkListDataError(e.getMessage());
                        } else if (e instanceof TokenInvalidException) {
                            mView.onLoadWorkListDataError(e.getMessage());
                        } else if (e instanceof SocketTimeoutException) {
                            mView.onLoadWorkListDataError("服务器连接超时!");
                        } else if (e instanceof IllegalStateException) {
                            mView.onLoadWorkListDataError("数据格式错误!");
                        } else {
                            mView.onLoadWorkListDataError(e.getMessage());
                        }
                    }
                }));
    }

    @Override
    void punchCard() {
        mRxManager.add(mModel.punchCard(mView.getAuthKey(),mView.getMaintenanceId(),
                mView.getWorkContentsJson(),mView.getConfirmJson(),mView.getCompleteRemark())
                .subscribeWith(new SimpleDisposableObserver<ApiModel<String>>(mView) {
                    @Override
                    public void onSucceed(Object o) {
                        String des = (String)o;
                        mView.onPunchCardSucceed();
                    }
                }));
    }
}